<!DOCTYPE html>
<html>
  <head>
  <meta charset="UTF-8">
    <title>Acceleration Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    
  </head>
  <body>
    <h1>Example</h1>
    <p>getCurrentAcceleration</p>
    <div id="x"></div>
    <br/>
    <div id="y"></div>
    <br/>
    <div id="z"></div>
    <br/>
    <div id="timestamp"></div>
    
    <script type="text/javascript" charset="utf-8">
		function $(str){
			return document.getElementById(str);
		}
		
		// watch id 是当前“watchAcceleration”的引用
		var watchID = null;
		
		// 等待加载PhoneGap
		document.addEventListener("deviceready", onDeviceReady, false);
		
		// PhoneGap加载完毕
		function onDeviceReady() {
			startWatch();
		}
		
		// 开始监视加速度
		function startWatch() {
		
		// 每隔3秒钟更新一次加速度数据
		var options = { frequency: 200 };
		
		watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
		}
		
		// 停止监视加速度
		function stopWatch() {
			if (watchID) {
				navigator.accelerometer.clearWatch(watchID);
				watchID = null;
			}
		}
	
		// onError: 获取加速度失败
		function onError() {
			alert('onError!');
		}
	
	    function onSuccess(acceleration) {
	  
	        	$("x").innerHTML=acceleration.x;
	        	$("y").innerHTML=acceleration.y;
	        	$("z").innerHTML=acceleration.z;
	        	$("timestamp").innerHTML=acceleration.timestamp;
	    }

    </script>
  </body>
</html>